package enterprise.manage.web.controller.order;

import enterprise.manage.dao.OrderDao;
import enterprise.manage.entity.Orders;
import enterprise.manage.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-25
 * Desc:
 */
public class OrderSaveController extends AbstractController {
    @Autowired
    OrderDao orderDao;
    @Autowired
    OrderService orderService;

    public static final String DEFAULT_PAGE = "order/view";

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {

        String id = request.getParameter("id").trim();
        String consignee = request.getParameter("consignee").trim();
        String consigneeAddress = request.getParameter("consigneeAddress").trim();
        String postcode = request.getParameter("postcode").trim();
        String mobile = request.getParameter("mobile").trim();
        String telephone = request.getParameter("telephone").trim();
        Orders entity = getEntity(id);


        entity.setConsignee(consignee);
        entity.setConsigneeAddress(consigneeAddress);
        entity.setPostcode(postcode);
        entity.setMobile(mobile);
        entity.setTelephone(telephone);
        orderService.save(entity);
        request.setAttribute("entity", entity);
        //成功跳转到 VIEW 页面
        request.setAttribute("method", "view");
        return new ModelAndView(DEFAULT_PAGE);
    }

    public Orders getEntity(String id) {
        if (StringUtils.isNotBlank(id)) {
            return orderDao.findByUnique("id", Long.parseLong(id));
        } else {
            return new Orders();
        }
    }


    public Object getEntity(Long id) {
        return orderDao.get(id);
    }


}